package com.example.jwt.main.utils;/*
 * 功能概要：日期工具
 *
 * [变更履历]
 * 2015年5月18日 上研院 姚宇华 新建
 *
 */

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 功能概要：日期工具<br>
 *
 * @author 上研院 姚宇华
 *
 */
public final class DateUtil {

    /**
     * yyyy
     * */
    public static final String FMT_DATE_YYYY = "yyyy";
    /**
     * yyyy-MM-dd
     * */
    public static final String FMT_DATE_YYYYMMDD = "yyyy-MM-dd";
    /**
     * yyyy/MM/dd
     * */
    public static final String FMT_DATE_YYYYMMDD_EXT = "yyyy/MM/dd";
    /**
     * yyyy.MM.dd
     * */
    public static final String FMT_DATE_YYYYMMDD_EXT2 = "yyyy.MM.dd";
    /**
     * yyyy-MM-dd HH:mm:ss
     * */
    public static final String FMT_DATE_YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    /**
     * HH:mm
     * */
    public static final String FMT_DATE_HHMM = "HH:mm";
    /**
     * HH:mm:ss
     * */
    public static final String FMT_DATE_HHMMSS = "HH:mm:ss";
    /**
     * yyyyMMdd
     * */
    public static final String FMT_DATE_SPECIAL = "yyyyMMdd";
    /**
     * MM-dd
     * */
    public static final String MT_DATE_MMDD = "MM-dd";
    /**
     * MMdd
     * */
    public static final String FMT_DATE_MMDD = "MMdd";
    /**
     * yyyy-MM-dd HH:mm
     * */
    public static final String FMT_DATE_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    /**
     * MM-dd HH:mm
     * */
    public static final String FMT_DATE_MMDD_HHMM = "MM-dd HH:mm";
    /**
     * MM月dd日
     * */
    public static final String FMT_DATE_MMMDDD = "MM月dd日";
    /**
     * yyyyMMddHHmm
     * */
    public static final String FMT_DATE_YYYYMMDDHHMM_NEW = "yyyyMMddHHmm";
    /**
     * yyyy年MM月dd日
     * */
    public static final String FMT_DATE_YYYY_MM_DD = "yyyy年MM月dd日";
    /**
     * yyyyMMddHHmmss
     * */
    public static final String FMT_DATE_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    /**
     * yyMMdd
     * */
    public static final String FMT_DATE_YYMMDD = "yyMMdd";
    /**
     * yyyyMMddHH
     * */
    public static final String FMT_DATE_YYMMDDHH = "yyyyMMddHH";
    /**
     * MM月dd日HH时mm分
     * */
    public static final String FMT_DATE_MMDD_HHMM_CH = "MM月dd日HH时mm分";


    /**
     *
     * 构造函数<br>
     *
     */
    private DateUtil() {

    }

    /**
     *
     * 创建金融交易附件文件夹<br>
     *
     * @return 结果
     */
    public static String creataFinancialTradeFolder() {
        return createTimeStamp() + (int) (10 + Math.random() * (99 - 10 + 1));
    }

    /**
     *
     * 给文件名加时间戳<br>
     *
     * @param file 文件名
     * @return 结果
     */
    public static String addTimeStamp(String file) {
        return createTimeStamp() + (int) (10 + Math.random() * (99 - 10 + 1)) + file;
    }

    /**
     *
     * 创建时间戳至秒<br>
     * 默认Format格式：yyyyMMddHHmmss <br>
     *
     * @return 结果
     */
    public static String createTimeStamp() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(System.currentTimeMillis());
    }

    /**
     *
     * 创建时间戳至秒<br>
     * 默认Format格式：yyyy-MM-dd HH:mm:ss <br>
     *
     * @add 2016-12-22 <br>
     * @return 结果
     */
    public static String createTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(System.currentTimeMillis());
    }

    /**
     *
     * 获取系统日期<br>
     *
     * @param format 时间格式
     * @return 结果
     */
    public static String getSysDate(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(System.currentTimeMillis());
    }

    /**
     *
     * 获取系统日期<br>
     *
     * @return 结果
     */
    public static Date getSysDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     *
     * 获取系统日期<br>
     *
     * @return 结果
     */
    public static String getSysDateStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(System.currentTimeMillis());
    }

    /**
     *
     * 创建时间戳至毫秒<br>
     *
     * @return 结果
     */
    public static String createTimeStampToMill() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return sdf.format(System.currentTimeMillis());
    }

    /**
     * [获取两个日期之间的分钟差]<br>
     *
     * @param beginDate 开始日
     * @param endDate 结束日
     * @return 小时
     * @throws Exception 异常
     */
    public static long minuteBetween(String beginDate, String endDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginDate.replace('/', '-')));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(endDate.replace('/', '-')));
        long time2 = cal.getTimeInMillis();
        long seconds = (long) (time2 - time1) / 1000;

        return seconds;
    }

    /**
     * [获取两个日期之间的小时差]<br>
     *
     * @param beginDate 开始日
     * @param endDate 结束日
     * @return 小时
     * @throws Exception 异常
     */
    public static float hoursBetween(String beginDate, String endDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginDate.replace('/', '-')));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(endDate.replace('/', '-')));
        long time2 = cal.getTimeInMillis();
        float betweenDays = (float) (time2 - time1) / (float) (1000 * 3600);

        return betweenDays;
    }

    /**
     * [获取两个日期之间的天数差]<br>
     *
     * @param beginDate 开始日
     * @param endDate 结束日
     * @return 天数差
     * @throws Exception 异常
     */
    public static int daysBetween(String beginDate, String endDate) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginDate.replace('/', '-')));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(endDate.replace('/', '-')));
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * [获取两个日期之间的天数差]<br>
     *
     * @param beginDate 开始日
     * @param endDate 结束日
     * @return 天数差
     * @throws Exception 异常
     */
    public static int daysBetween(Date beginDate, Date endDate) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * [根据日期和天数间隔获取对应日期]<br>
     *
     * @param beginDate 开始日
     * @param days 间隔天数
     * @return 日期
     * @throws Exception 异常
     */
    public static Date getDate(String beginDate, int days) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginDate.replace('/', '-')));
        long time1 = cal.getTimeInMillis();
        long time2 = time1 + (long) days * 1000 * 3600 * 24;

        return new Date(time2);
    }

    /**
     * [根据日期和天数间隔获取对应日期]<br>
     *
     * @param beginDate 开始日
     * @param days 间隔天数
     * @return 日期
     * @throws Exception 异常
     */
    public static Date getDate(Date beginDate, int days) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        long time1 = cal.getTimeInMillis();
        long time2 = time1 + (long) days * 1000 * 3600 * 24;

        return new Date(time2);
    }

    /**
     * [把字符串转化为Date类型]<br>
     *
     * @param dateStr 日期字符串
     * @return 日期对象
     * @throws Exception 异常
     */
    public static Date getDateFromStr(String dateStr) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = df.parse(dateStr.replace('/', '-'));
        return date;
    }

    /**
     * [获取两个日期之间的月数]<br>
     *
     * @param beginDate 开始日
     * @param endDate 结束日
     * @return 天数差
     * @throws Exception 异常
     */
    public static int getmonthsBetween(Date beginDate, Date endDate) throws Exception {

        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        int yearStart = cal.get(Calendar.YEAR) - 1900;
        int monthStart = cal.get(Calendar.MONTH);
        int dayStart = cal.get(Calendar.DAY_OF_MONTH);

        cal.setTime(endDate);
        int yearEnd = cal.get(Calendar.YEAR) - 1900;
        int monthEnd = cal.get(Calendar.MONTH);
        int dayEnd = cal.get(Calendar.DAY_OF_MONTH);

        /*
         * 获取月数的方案 持有月数=（X-M）*12+（Y-N）+修正值 当Z+15-K≥1时，修正值是0 当Z+15-K<1时，修正值是-1
         */
        int modifiedValue = 0;
        if (dayEnd - dayStart + 15 < 1) {
            modifiedValue = -1;
        }

        return (yearEnd - yearStart) * 12 + (monthEnd - monthStart) + modifiedValue;

    }

    /**
     *
     * [获取当前时间，格式yyyyMMddHHmmss]<br>
     *
     * @return 返回值
     */
    public static String getCurrentDateTimeStr() {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String timeString = dataFormat.format(date);
        return timeString;
    }

    /**
     *
     * 时间转换<br>
     *
     * @param date date
     * @return 结果
     */
    public static String parseDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (null != date) {
            return sdf.format(date);
        } else {
            return null;
        }
    }

    /**
     *
     * 时间转换<br>
     *
     * @param date 日期
     * @param format 格式
     * @return 结果
     * @exception Exception 异常
     */
    public static Date string2Date(String date, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    /**
     *
     * 时间转换<br>
     *
     * @param date date
     * @return 结果
     */
    public static String parseDateStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (null != date) {
            return sdf.format(date);
        } else {
            return null;
        }
    }

    /**
     *
     * 时间转换<br>
     *
     * @param date date
     * @return 结果
     */
    public static String formatYmdhm(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (null != date) {
            return sdf.format(date);
        } else {
            return null;
        }
    }

    /**
     *
     * [时间格式化]<br>
     *
     * @param fmtDate date
     * @return 结果
     * @exception ParseException 日期转换异常
     */
    public static String formatYmdhm(String fmtDate) throws ParseException {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (!StringUtil.isEmpty(fmtDate)) {
            return dataFormat.format(dataFormat.parse(fmtDate));
        } else {
            return null;
        }
    }

    /**
     *
     * [时间格式化]<br>
     *
     * @param fmtDate date
     * @return 结果
     */
    public static String getFormatDate(String fmtDate) {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        String timeString = "";
        if (StringUtil.isEmpty(fmtDate)) {
            Date date = new Date();
            timeString = dataFormat.format(date);
        } else {
            timeString = dataFormat.format(fmtDate);
        }
        return timeString;

    }

    /**
     * 获取当前时间几天前的时间戳
     *
     * @param daysBefore 几天前
     * @return 时间戳
     * */
    public static long getDaysBefore(Integer daysBefore) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DAY_OF_MONTH, 0 - daysBefore);
        return c.getTimeInMillis();
    }

    /**
     * 获取当前时间几分钟前的时间戳
     *
     * @param minutesBefore 几分钟前
     * @return 时间戳
     * */
    public static long getMinuteBefore(Integer minutesBefore) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.MINUTE, 0 - minutesBefore);
        return c.getTimeInMillis();
    }

    /**
     * 日期加减计算<br>
     *
     * @param date 日期
     * @param day 天数
     * @param format 格式信息
     * @throws ParseException 异常
     * @return 计算后的日期(type = string)
     *
     */
    public static String addDay(String date, int day, String format) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat(format);
        Date d = new Date(f.parse(date).getTime() + new BigDecimal(24 * 3600 * 1000).multiply(new BigDecimal(day)).longValue());
        return f.format(d);
    }

    /**
     * 日期加减计算<br>
     *
     * @param date 日期
     * @param day 天数
     * @param format 格式信息
     * @throws ParseException 异常
     * @return 计算后的日期(type = date)
     *
     */
    public static Date addDay4Date(String date, int day, String format) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat(format);
        return new Date(f.parse(date).getTime() + new BigDecimal(24 * 3600 * 1000).multiply(new BigDecimal(day)).longValue());
    }

    /**
     * 日期减计算<br>
     *
     * @param date 日期
     * @param day 天数
     * @throws ParseException 异常
     * @return 计算后的日期
     *
     */
    public static String subtractDay(String date, int day) throws ParseException {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date(new BigDecimal(f.parse(date).getTime()).subtract(new BigDecimal(day * 24 * 3600 * 1000)).longValue());
        return f.format(d);
    }

    /**
     * 获得开始到结束之间的所有日期yyyy/MM/dd
     *
     * @param beginStr 开始日期
     * @param endStr 结束日期
     * @return dateList
     * @throws ParseException 异常
     */
    public static List<String> getDateList(String beginStr, String endStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        List<String> dateList = new ArrayList<String>();
        Date begin = sdf.parse(beginStr);
        Date end = sdf.parse(endStr);
        Calendar calb = Calendar.getInstance();
        calb.setTime(begin);
        Calendar cale = Calendar.getInstance();
        cale.setTime(end);
        while (calb.getTimeInMillis() <= cale.getTimeInMillis()) {
            dateList.add(sdf.format(calb.getTime()));
            calb.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dateList;
    }

    /**
     *
     * [时间格式化]<br>
     *
     * @param fmtDate date
     * @return 结果
     * @exception ParseException 日期转换异常
     */
    public static Date fmtStringToDate(String fmtDate) throws ParseException {
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date resultDate = dataFormat.parse(fmtDate);
        return resultDate;
    }

    /**
     * String类型日期转Date
     *
     * @param date date
     * @return Date
     * @throws ParseException 异常
     */
    public static Date string2Date(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date resultDate = sdf.parse(date);
        return resultDate;
    }

    /**
     * Date类型日期转String
     *
     * @param date date
     * @return 结果
     * @throws ParseException 异常
     */
    public static String date2String(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = sdf.format(date);
        return currentTime;
    }

    /**
     * [根据日期和天数间隔获取对应日期]<br>
     *
     * @param beginDate 开始日
     * @param days 间隔天数
     * @return 日期
     * @throws ParseException 异常
     */
    public static String getDiffDate(String beginDate, int days) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(beginDate.replace('/', '-')));
        long time1 = cal.getTimeInMillis();
        long time2 = time1 + (long) days * 1000 * 3600 * 24;
        // 进行时间转换
        String date = sdf.format(new Date(time2));
        return date;
    }

    /**
     * [当前日期和传入日期相差的分钟] <br>
     * Math.abs() <br>
     *
     * @param dataTime 日期
     * @return long 0
     * @throws ParseException 异常
     */
    public static long getMinutes(String dataTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(dataTime));
        long time1 = cal.get(Calendar.MINUTE);
        cal.setTime(new Date());
        long time2 = cal.get(Calendar.MINUTE);
        long minutes = time2 - time1;
        return Math.abs(minutes);
    }

    /**
     * [当前日期和传入日期相差的秒数] <br>
     * Math.abs() <br>
     *
     * @param dataTime 日期
     * @return long 0
     * @throws ParseException 异常
     */
    public static long getSeconds(String dataTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(dataTime));
        long time1 = cal.getTimeInMillis();
        cal.setTime(new Date());
        long time2 = cal.getTimeInMillis();
        long seconds = time2 - time1;
        return Math.abs(seconds) / 1000;
    }

    /**
     *
     * [判断now日期是否在两个date之间]<br>
     *
     * @param begin 开始日期
     * @param end 结束日期
     * @param now 当前时间
     * @return 是否
     * @throws ParseException 异常
     */
    public static boolean isDateBetween(Date begin, Date end, String now) throws ParseException {
        Date nowDate = string2Date(now);
        return nowDate.compareTo(begin) >= 0 && nowDate.compareTo(end) <= 0;
    }

}
